package com.vhall.component.service.preparation.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vhall.component.dao.preparation.ScrollingMapper;
import com.vhall.component.entity.preparation.ScrollingEntity;
import com.vhall.component.entity.preparation.vo.ScrollingSaveVO;
import com.vhall.component.entity.preparation.vo.ScrollingVO;
import com.vhall.component.entity.room.dto.RoomsEntityDTO;
import com.vhall.component.entity.subject.enums.StatusEnum;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.enums.RoomConfigStatusEnum;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.service.preparation.ScrollingConsoleService;
import com.vhall.component.service.room.RoomInnerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author longzhen.gong
 */
@Service
public class ScrollingConsoleServiceImpl implements ScrollingConsoleService {

    @Autowired
    private RoomInnerService roomInnerService;

    @Autowired
    private ScrollingMapper scrollingMapper;


    @Override
    public ScrollingVO save(ScrollingSaveVO reqVo) {
        RoomsEntityDTO room = roomInnerService.getRoomInfo(reqVo.getRoomId());
        if (room == null) {
            throw new BusinessException(BizErrorCode.EMPTY_ROOM);
        }
        if ("1".equals(String.valueOf(room.getStatus()))) {
            throw new BusinessException(BizErrorCode.BIZ_NOT_EDIT);
        }
        ScrollingEntity scrollingEntity = getScrolling(reqVo.getRoomId());
        if (scrollingEntity != null) {
            scrollingEntity = buildScroll(reqVo, scrollingEntity);
            scrollingEntity.setUpdatedAt(LocalDateTime.now());
            scrollingMapper.updateById(scrollingEntity);
        } else {
            scrollingEntity = buildScroll(reqVo, scrollingEntity);
            scrollingMapper.insert(scrollingEntity);
        }

        ScrollingVO scrollingVO = new ScrollingVO();
        BeanUtils.copyProperties(scrollingEntity, scrollingVO);

        //判断是否开启跑马灯

        roomInnerService.updateConfigValueByRoomId(scrollingEntity.getRoomId(), scrollingEntity.getStatus().equals(StatusEnum.OPEN.getValue())
                ? StatusEnum.OPEN.getValue() : StatusEnum.CLOSE.getValue(), RoomConfigStatusEnum.SCROLLING_STATUS.getValue());

        return scrollingVO;

    }

    private ScrollingEntity getScrolling(String roomId) {
        LambdaQueryWrapper<ScrollingEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ScrollingEntity::getRoomId, roomId);
        wrapper.isNull(ScrollingEntity::getDeletedAt);
        wrapper.last("limit 1;");
        return scrollingMapper.selectOne(wrapper);
    }

    @Override
    public ScrollingVO info(String roomId) {
        RoomsEntityDTO room = roomInnerService.getRoomInfo(roomId);
        if (room == null) {
            throw new BusinessException(BizErrorCode.EMPTY_ROOM);
        }
        ScrollingEntity entity = getScrolling(roomId);
        if (null == entity) {
            return null;
        }
        ScrollingVO vo = new ScrollingVO();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    /**
     * 构造对象
     *
     * @param reqVo
     * @param scroll
     * @return
     */
    private ScrollingEntity buildScroll(ScrollingSaveVO reqVo, ScrollingEntity scroll) {
        if (scroll == null) {
            scroll = new ScrollingEntity();
        }

        if (!StringUtils.isBlank(reqVo.getRoomId())) {
            scroll.setRoomId(reqVo.getRoomId());
        }
        if (reqVo.getScrollingOpen() != null) {
            scroll.setScrollingOpen(reqVo.getScrollingOpen());
        }

        if (!StringUtils.isBlank(reqVo.getText())) {
            scroll.setText(reqVo.getText());
        } else {
            scroll.setText("");
        }
        if (reqVo.getTextType() != null) {
            scroll.setTextType(reqVo.getTextType());
        }

        if (reqVo.getAlpha() != null) {
            scroll.setAlpha(reqVo.getAlpha());
        }
        if (reqVo.getSize() != null) {
            scroll.setSize(reqVo.getSize());
        }

        if (!StringUtils.isBlank(reqVo.getColor())) {
            scroll.setColor(reqVo.getColor());
        }

        if (reqVo.getInterval() != null) {
            scroll.setInterval(reqVo.getInterval());
        }
        if (reqVo.getSpeed() != null) {
            scroll.setSpeed(reqVo.getSpeed());
        }

        if (reqVo.getPosition() != null) {
            scroll.setPosition(reqVo.getPosition());

        } else {
            scroll.setPosition(1);
        }

        if (reqVo.getStatus() != null) {
            scroll.setStatus(reqVo.getStatus());
        }

        if (reqVo.getType() != null) {
            if (reqVo.getType() == 1) {
                scroll.setStatus(0);
            }
        }
        return scroll;
    }
}
